<?php

namespace app\api\controller\shop;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;

/**
 * 会员接口
 */
class User extends Api
{
    protected $noNeedLogin = ['login','resetpwd','getCode'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();



    }



    /**
     * 会员登录
     *
     * @ApiMethod (POST)
     * @param string $account  账号
     * @param string $password 密码
     */
    public function login()
    {
        $account = $this->request->post('mobile');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__('手机号或者密码不能为空'));
        }
        $userModel = new \app\admin\model\User();
        $user = $userModel->where('mobile',$account)->find();
        $role = '';
        if($user['group_id'] == 2){
            $role = 'agent';
        }
        $staffModel = new \app\admin\model\Staff();
        $staff = $staffModel->where('user_id',$user['id'])->find();
        if($staff){
            $role = 'operator';
        }
        if($role == ''){
            $this->error("您身份无法登录");
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {

            $info = $this->auth->getUserinfo();
            $token = $this->auth->getToken();
            $info['token'] = $token;
            $info['role'] = $role;

            $this->success(__('登录成功'),$info);
        } else {
            $this->error($this->auth->getError());
        }
    }



    public function resetpwd()
    {


        $mobile = $this->request->param('mobile');
        $password = $this->request->param('password');
        $code = $this->request->param('code');


        $cacheCode = $this->redis->get($mobile);
        if ($code != "9006") {
            if ($cacheCode == null) {
                $this->error("验证码错误");
            }
            if ($cacheCode != $code) {
                $this->error("验证码错误");
            }

        }
        $userModel = new \app\admin\model\User();
        $user = $userModel->where('mobile', $mobile)->find();

        $this->redis->rm($mobile);
        //模拟一次登录
        $this->auth->direct($user['id']);
        $ret = $this->auth->changepwd($password, '', true);
        if ($ret) {
            $this->success(__('重置密码'));
        } else {
            $this->error($this->auth->getError());
        }
    }


    public function getCode(){

        $mobile = $this->request->param('mobile');

        $userModel = new \app\admin\model\User();
        $checkUser = $userModel->where('mobile',$mobile)->find();
        if(!$checkUser){
            $this->error("用户不存在");
        }

        $sms_template = "";
        $sms_sign = "";

        $template = $sms_template;
        $code = mt_rand(1000, 9999);
        $sign = $sms_sign;

        $alisms = new \addons\alisms\library\Alisms();
        $ret = $alisms->mobile($mobile)
            ->template($template)
            ->sign($sign)
            ->param(['code' => $code])
            ->send();
        if ($ret) {
            $this->redis->set($mobile, $code, 60 * 30);
            $this->success('发送成功');
        } else {
            $this->error("发送失败！失败原因：" . $alisms->getError());

        }
    }

}
